/**
* Title : Rolling News
* Company : motionj.com 
* Author : Son Jeong-Min (motionj.com)
* Email : toto@motionj.com
* URL : http://www.motionj.com
* Version : v1.0
* License : Free
* Description :
**/

(function($) {
	var no = 0;
	var pause = false;
	var count;

	var methods = {
		// init options
		_init : function( options ){
			var	defaults = {
				animator : "> ul > li",
				speed : 1000,
				delay : 3000,
				viewLine : 5,
				maxLine : 10,
				viewPopup : true
			}
			var opt = $.extend(defaults, options);
			return opt;
		},
		// css set
		_set : function(o, e){
			var a = e.find(o.animator);
			count = a.length;
			var min_height = a.outerHeight(true);
			var layer_code = '';
			if(o.viewPopup) layer_code += '<div class="motionJ_popLayer">';
			a.each( function(i){
				if(o.viewPopup) {
					if(i < o.maxLine){
						layer_code += '<p>';
						layer_code += $(this).html();
						layer_code += '</p>';
					}
				}

				$(this).css({
					position : "absolute",
					top		 : min_height*i
				});
			});
			if(o.viewPopup) layer_code += '</div>';
			var height = min_height * o.viewLine;
			height += "px";
			e.children('ul').css({
				"height" : height,
				"overflow" : "hidden",
				"position" : "relative"
			});
			e.css({
				"height" : height,
				"position" : "relative"
			});
			if(o.viewPopup) $(layer_code).appendTo(e);
			e.mouseover( function(){ pause = true; })
			e.mouseleave( function(){ pause = false; });
			e.children('ul').mouseover( function(){
				e.children('.motionJ_popLayer').show();
			});
			e.mouseleave( function(){ 
				e.children('.motionJ_popLayer').hide();
			});
			return min_height; 
		},
		// act!!!
		_act : function(o, e, a, h){
			if(!pause){
				a.each( function(i){
					var a_t = $(this).css('top');
					a_t = a_t.substring(0, a_t.length-2);
					a_t = parseInt(a_t);
					var loop = false;
					e.find('ul li:eq('+i+')').animate({top: (a_t-h)}, o.speed,  function(){
						if(loop == false){
							e.find('ul li:eq('+ (no-1) +')').css('top',((count-1) * h));
							loop == true;
						}
					});
				});
				
				if(no == count) no = 1;
				else no++;
			}
		},
		_move : function(o, e, a, h){
			e.find('.nav_wrap span').each(function(j){
				$(this).click(function(){
					if(e.find('ul li:animated').length == 0){
						//j == 0 : up
						//j == 1 : down
						if(j == 0){
							a.each( function(i){
								var a_t = $(this).css('top');
								a_t = a_t.substring(0, a_t.length-2);
								a_t = parseInt(a_t);
								var loop = false;
								e.find('ul li:eq('+i+')').animate({top: (a_t-h)}, o.speed/2 ,  function(){
									if(loop == false){
										e.find('ul li:eq('+ (no-1) +')').css('top',((count-1) * h));
										loop == true;
									}
								});
							});
							if(no == count) no = 1;
							else no++;
						}else{
							if(no <= 0) no = (count-1);
							else no--;
							e.find('ul li:eq('+ Math.abs(no) +')').css('top',(-h)+'px');
							a.each( function(i){
								var a_t = $(this).css('top');
								a_t = a_t.substring(0, a_t.length-2);
								a_t = parseInt(a_t);
								e.find('ul li:eq('+i+')').animate({top: (a_t+h)}, o.speed/2 );
							});
						}
					}
				});
			});
		}
	}

	jQuery.fn.motionJ_flashNews = function( method ) {
		var _this = this;
		var option = methods._init( method );
		var height = methods._set(option, _this);
		methods._move(option, _this, _this.find(option.animator), height);
		setInterval( function () { methods._act(option, _this, _this.find(option.animator), height) }, option.delay);
	}
	
})(jQuery);